package com.openbravo.pos.catalog;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.JMessageDialog;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.sales.TaxesLogic;
import com.openbravo.pos.ticket.CategoryInfo;
import com.openbravo.pos.ticket.ProductInfoExt;
import com.openbravo.pos.ticket.TaxInfo;
import com.openbravo.pos.util.ThumbNailBuilder;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JCatalog extends JPanel
  implements ListSelectionListener, CatalogSelector
{
  protected EventListenerList listeners = new EventListenerList();
  private DataLogicSales m_dlSales;
  private TaxesLogic taxeslogic;
  private boolean pricevisible;
  private boolean taxesincluded;
  private Map<String, ProductInfoExt> m_productsset = new HashMap();
  private Set<String> m_categoriesset = new HashSet();
  private ThumbNailBuilder tnbbutton;
  private ThumbNailBuilder tnbcat;
  private CategoryInfo showingcategory = null;
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JPanel jPanel4;
  private JPanel jPanel5;
  private JButton m_btnBack1;
  private JPanel m_jCategories;
  private JButton m_jDown;
  private JList m_jListCategories;
  private JPanel m_jProducts;
  private JPanel m_jRootCategories;
  private JPanel m_jSubCategories;
  private JButton m_jUp;
  private JScrollPane m_jscrollcat;
  private JLabel m_lblIndicator;
private JCatalogTab localObject1;

  public JCatalog(DataLogicSales paramDataLogicSales)
  {
    this(paramDataLogicSales, false, false, 64, 54);
  }

  public JCatalog(DataLogicSales paramDataLogicSales, boolean paramBoolean1, boolean paramBoolean2, int paramInt1, int paramInt2)
  {
    this.m_dlSales = paramDataLogicSales;
    this.pricevisible = paramBoolean1;
    this.taxesincluded = paramBoolean2;
    initComponents();
    this.m_jListCategories.addListSelectionListener(this);
    this.m_jscrollcat.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
    this.tnbcat = new ThumbNailBuilder(32, 32, "com/openbravo/images/folder_yellow.png");
    this.tnbbutton = new ThumbNailBuilder(paramInt1, paramInt2, "com/openbravo/images/package.png");
  }

  public Component getComponent()
  {
    return this;
  }

  public void showCatalogPanel(String paramString)
  {
    if (paramString == null)
      showRootCategoriesPanel();
    else
      showProductPanel(paramString);
  }

  public void loadCatalog()
    throws BasicException
  {
    this.m_jProducts.removeAll();
    this.m_productsset.clear();
    this.m_categoriesset.clear();
    this.showingcategory = null;
   // this.taxeslogic = new TaxesLogic(this.m_dlSales.getTaxList().list());
    //com.openbravo.pos.forms.DataLogicSales@618a63  m_dlSales
    List localList = this.m_dlSales.getRootCategories();
    this.m_jListCategories.setCellRenderer(new SmallCategoryRenderer( ));
    this.m_jListCategories.setModel(new CategoriesListModel(localList));
    if (this.m_jListCategories.getModel().getSize() == 0)
    {
      this.m_jscrollcat.setVisible(true);//ati for mysql
      this.jPanel2.setVisible(true);
    }
    else
    {
      this.m_jscrollcat.setVisible(true);
      this.jPanel2.setVisible(true);
      this.m_jListCategories.setSelectedIndex(0);
    }
    showRootCategoriesPanel();
  }

  public void setComponentEnabled(boolean paramBoolean)
  {
    this.m_jListCategories.setEnabled(paramBoolean);
    this.m_jscrollcat.setEnabled(paramBoolean);
    this.m_jUp.setEnabled(paramBoolean);
    this.m_jDown.setEnabled(paramBoolean);
    this.m_lblIndicator.setEnabled(paramBoolean);
    this.m_btnBack1.setEnabled(paramBoolean);
    this.m_jProducts.setEnabled(paramBoolean);
    synchronized (this.m_jProducts.getTreeLock())
    {
      int i = this.m_jProducts.getComponentCount();
      for (int j = 0; j < i; j++)
        this.m_jProducts.getComponent(j).setEnabled(paramBoolean);
    }
    setEnabled(paramBoolean);
  }

  public void addActionListener(ActionListener paramActionListener)
  {
    this.listeners.add(ActionListener.class, paramActionListener);
  }

  public void removeActionListener(ActionListener paramActionListener)
  {
    this.listeners.remove(ActionListener.class, paramActionListener);
  }

  public void valueChanged(ListSelectionEvent paramListSelectionEvent)
  {
    if (!paramListSelectionEvent.getValueIsAdjusting())
    {
      int i = this.m_jListCategories.getSelectedIndex();
      if (i >= 0)
      {
        Rectangle localRectangle = this.m_jListCategories.getCellBounds(i, i);
        this.m_jListCategories.scrollRectToVisible(localRectangle);
      }
    }
  }

  protected void fireSelectedProduct(ProductInfoExt paramProductInfoExt)
  {
	  //[com.openbravo.pos.sales.JPanelTicketSales$CatalogListener@13212fc]
    EventListener[] arrayOfEventListener = this.listeners.getListeners(ActionListener.class);
    ActionEvent localActionEvent = null;
    for (int i = 0; i < arrayOfEventListener.length; i++)
    {
      if (localActionEvent == null)
        localActionEvent = new ActionEvent(paramProductInfoExt, 1001, paramProductInfoExt.getID());
      
      ActionListener actionListener = (ActionListener)arrayOfEventListener[i];
	actionListener.actionPerformed(localActionEvent);
    }
  }

  private void selectCategoryPanel(String paramString)
  {
    try
    {
      if (!this.m_categoriesset.contains(paramString))
      {
        localObject1 = new JCatalogTab();
        ((JCatalogTab)localObject1).applyComponentOrientation(getComponentOrientation());
        this.m_jProducts.add((Component)localObject1, paramString);
        this.m_categoriesset.add(paramString);
        List localList = this.m_dlSales.getSubcategories(paramString);
        Object localObject2 = localList.iterator();
        while (((Iterator)localObject2).hasNext())
        {
        	CategoryInfo   localObject3 = (CategoryInfo)((Iterator)localObject2).next();
          ((JCatalogTab)localObject1).addButton(new ImageIcon(this.tnbbutton.getThumbNailText(((CategoryInfo)localObject3).getImage(), ((CategoryInfo)localObject3).getName())), new SelectedCategory((CategoryInfo)localObject3));
        }
        localObject2 = this.m_dlSales.getProductCatalog(paramString);
        Object localObject3 = ((List)localObject2).iterator();
        while (((Iterator)localObject3).hasNext())
        {
          ProductInfoExt localProductInfoExt = (ProductInfoExt)((Iterator)localObject3).next();
          ((JCatalogTab)localObject1).addButton(new ImageIcon(this.tnbbutton.getThumbNailText(localProductInfoExt.getImage(), getProductLabel(localProductInfoExt))), new SelectedAction(localProductInfoExt));
        }
      }
      Object localObject1 = (CardLayout)(CardLayout)this.m_jProducts.getLayout();
      ((CardLayout)localObject1).show(this.m_jProducts, paramString);
    }
    catch (BasicException localBasicException)
    {
      JMessageDialog.showMessage(this, new MessageInf(-33554432, AppLocal.getIntString("message.notactive"), localBasicException));
    }
  }

  private String getProductLabel(ProductInfoExt paramProductInfoExt)
  {
    if (this.pricevisible)
    {
      if (this.taxesincluded)
      {
        TaxInfo localTaxInfo = this.taxeslogic.getTaxInfo(paramProductInfoExt.getTaxCategoryID(), new Date());
        return "<html><center>" + paramProductInfoExt.getName() + "<br>" + paramProductInfoExt.printPriceSellTax(localTaxInfo);
      }
      return "<html><center>" + paramProductInfoExt.getName() + "<br>" + paramProductInfoExt.printPriceSell();
    }
    return paramProductInfoExt.getName();
  }

  private void selectIndicatorPanel(Icon paramIcon, String paramString)
  {
    this.m_lblIndicator.setText(paramString);
    this.m_lblIndicator.setIcon(paramIcon);
    CardLayout localCardLayout = (CardLayout)(CardLayout)this.m_jCategories.getLayout();
    localCardLayout.show(this.m_jCategories, "subcategories");
  }

  private void selectIndicatorCategories()
  {
    CardLayout localCardLayout = (CardLayout)(CardLayout)this.m_jCategories.getLayout();
    localCardLayout.show(this.m_jCategories, "rootcategories");
  }

  private void showRootCategoriesPanel()
  {
    selectIndicatorCategories();
    CategoryInfo localCategoryInfo = (CategoryInfo)this.m_jListCategories.getSelectedValue();
    if (localCategoryInfo != null)
      selectCategoryPanel(localCategoryInfo.getID());
    this.showingcategory = null;
  }

  private void showSubcategoryPanel(CategoryInfo paramCategoryInfo)
  {
    selectIndicatorPanel(new ImageIcon(this.tnbbutton.getThumbNail(paramCategoryInfo.getImage())), paramCategoryInfo.getName());
    selectCategoryPanel(paramCategoryInfo.getID());
    this.showingcategory = paramCategoryInfo;
  }

  private void showProductPanel(String paramString)
  {
    ProductInfoExt localProductInfoExt1 = (ProductInfoExt)this.m_productsset.get(paramString);
    if (localProductInfoExt1 == null)
    {
      if (this.m_productsset.containsKey(paramString))
      {
        if (this.showingcategory == null)
          showRootCategoriesPanel();
        else
          showSubcategoryPanel(this.showingcategory);
      }
      else
        try
        {
          List localList = this.m_dlSales.getProductComments(paramString);
          if (localList.size() == 0)
          {
            this.m_productsset.put(paramString, null);
            if (this.showingcategory == null)
              showRootCategoriesPanel();
            else
              showSubcategoryPanel(this.showingcategory);
          }
          else
          {
            localProductInfoExt1 = this.m_dlSales.getProductInfo(paramString);
            this.m_productsset.put(paramString, localProductInfoExt1);
            JCatalogTab localJCatalogTab = new JCatalogTab();
            localJCatalogTab.applyComponentOrientation(getComponentOrientation());
            this.m_jProducts.add(localJCatalogTab, "PRODUCT." + paramString);
            Object localObject = localList.iterator();
            while (((Iterator)localObject).hasNext())
            {
              ProductInfoExt localProductInfoExt2 = (ProductInfoExt)((Iterator)localObject).next();
              localJCatalogTab.addButton(new ImageIcon(this.tnbbutton.getThumbNailText(localProductInfoExt2.getImage(), getProductLabel(localProductInfoExt2))), new SelectedAction(localProductInfoExt2));
            }
            selectIndicatorPanel(new ImageIcon(this.tnbbutton.getThumbNail(localProductInfoExt1.getImage())), localProductInfoExt1.getName());
            localObject = (CardLayout)(CardLayout)this.m_jProducts.getLayout();
            ((CardLayout)localObject).show(this.m_jProducts, "PRODUCT." + paramString);
          }
        }
        catch (BasicException localBasicException)
        {
          localBasicException.printStackTrace();
          this.m_productsset.put(paramString, null);
          if (this.showingcategory == null)
            showRootCategoriesPanel();
          else
            showSubcategoryPanel(this.showingcategory);
        }
    }
    else
    {
      selectIndicatorPanel(new ImageIcon(this.tnbbutton.getThumbNail(localProductInfoExt1.getImage())), localProductInfoExt1.getName());
      CardLayout localCardLayout = (CardLayout)(CardLayout)this.m_jProducts.getLayout();
      localCardLayout.show(this.m_jProducts, "PRODUCT." + paramString);
    }
  }

  private void initComponents()
  {
    this.m_jCategories = new JPanel();
    this.m_jRootCategories = new JPanel();
    this.m_jscrollcat = new JScrollPane();
    this.m_jListCategories = new JList();
    this.jPanel2 = new JPanel();
    this.jPanel3 = new JPanel();
    this.m_jUp = new JButton();
    this.m_jDown = new JButton();
    this.m_jSubCategories = new JPanel();
    this.jPanel4 = new JPanel();
    this.m_lblIndicator = new JLabel();
    this.jPanel1 = new JPanel();
    this.jPanel5 = new JPanel();
    this.m_btnBack1 = new JButton();
    this.m_jProducts = new JPanel();
    setLayout(new BorderLayout());
    this.m_jCategories.setMaximumSize(new Dimension(275, 600));
    this.m_jCategories.setPreferredSize(new Dimension(275, 600));
    this.m_jCategories.setLayout(new CardLayout());
    this.m_jRootCategories.setLayout(new BorderLayout());
    this.m_jscrollcat.setHorizontalScrollBarPolicy(31);
    this.m_jscrollcat.setVerticalScrollBarPolicy(22);
    this.m_jListCategories.setSelectionMode(0);
    this.m_jListCategories.setFocusable(false);
    this.m_jListCategories.addListSelectionListener(new ListSelectionListener()
    {
      public void valueChanged(ListSelectionEvent paramListSelectionEvent)
      {
        JCatalog.this.m_jListCategoriesValueChanged(paramListSelectionEvent);
      }
    });
    this.m_jscrollcat.setViewportView(this.m_jListCategories);
    this.m_jRootCategories.add(this.m_jscrollcat, "Center");
    this.jPanel2.setLayout(new BorderLayout());
    this.jPanel3.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    this.jPanel3.setLayout(new GridLayout(0, 1, 0, 5));
    this.m_jUp.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/1uparrow22.png")));
    this.m_jUp.setFocusPainted(false);
    this.m_jUp.setFocusable(false);
    this.m_jUp.setMargin(new Insets(8, 14, 8, 14));
    this.m_jUp.setRequestFocusEnabled(false);
    this.m_jUp.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCatalog.this.m_jUpActionPerformed(paramActionEvent);
      }
    });
    this.jPanel3.add(this.m_jUp);
    this.m_jDown.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/1downarrow22.png")));
    this.m_jDown.setFocusPainted(false);
    this.m_jDown.setFocusable(false);
    this.m_jDown.setMargin(new Insets(8, 14, 8, 14));
    this.m_jDown.setRequestFocusEnabled(false);
    this.m_jDown.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCatalog.this.m_jDownActionPerformed(paramActionEvent);
      }
    });
    this.jPanel3.add(this.m_jDown);
    this.jPanel2.add(this.jPanel3, "North");
    this.m_jRootCategories.add(this.jPanel2, "After");
    this.m_jCategories.add(this.m_jRootCategories, "rootcategories");
    this.m_jSubCategories.setLayout(new BorderLayout());
    this.jPanel4.setLayout(new BorderLayout());
    this.m_lblIndicator.setText("jLabel1");
    this.jPanel4.add(this.m_lblIndicator, "North");
    this.m_jSubCategories.add(this.jPanel4, "Center");
    this.jPanel1.setLayout(new BorderLayout());
    this.jPanel5.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    this.jPanel5.setLayout(new GridLayout(0, 1, 0, 5));
    this.m_btnBack1.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/3uparrow2.png")));
    this.m_btnBack1.setFocusPainted(false);
    this.m_btnBack1.setFocusable(false);
    this.m_btnBack1.setMargin(new Insets(8, 14, 8, 14));
    this.m_btnBack1.setRequestFocusEnabled(false);
    this.m_btnBack1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCatalog.this.m_btnBack1ActionPerformed(paramActionEvent);
      }
    });
    this.jPanel5.add(this.m_btnBack1);
    this.jPanel1.add(this.jPanel5, "North");
    this.m_jSubCategories.add(this.jPanel1, "After");
    this.m_jCategories.add(this.m_jSubCategories, "subcategories");
    add(this.m_jCategories, "Before");
    this.m_jProducts.setLayout(new CardLayout());
    add(this.m_jProducts, "Center");
  }

  private void m_jDownActionPerformed(ActionEvent paramActionEvent)
  {
    int i = this.m_jListCategories.getSelectionModel().getMaxSelectionIndex();
    if (i < 0)
    {
      i = 0;
    }
    else
    {
      i++;
      if (i >= this.m_jListCategories.getModel().getSize())
        i = this.m_jListCategories.getModel().getSize() - 1;
    }
    if ((i >= 0) && (i < this.m_jListCategories.getModel().getSize()))
      this.m_jListCategories.getSelectionModel().setSelectionInterval(i, i);
  }

  private void m_jUpActionPerformed(ActionEvent paramActionEvent)
  {
    int i = this.m_jListCategories.getSelectionModel().getMinSelectionIndex();
    if (i < 0)
    {
      i = this.m_jListCategories.getModel().getSize() - 1;
    }
    else
    {
      i--;
      if (i < 0)
        i = 0;
    }
    if ((i >= 0) && (i < this.m_jListCategories.getModel().getSize()))
      this.m_jListCategories.getSelectionModel().setSelectionInterval(i, i);
  }

  private void m_jListCategoriesValueChanged(ListSelectionEvent paramListSelectionEvent)
  {
    if (!paramListSelectionEvent.getValueIsAdjusting())
    {
      CategoryInfo localCategoryInfo = (CategoryInfo)this.m_jListCategories.getSelectedValue();
      if (localCategoryInfo != null)
        selectCategoryPanel(localCategoryInfo.getID());
    }
  }

  private void m_btnBack1ActionPerformed(ActionEvent paramActionEvent)
  {
    showRootCategoriesPanel();
  }

  private class SmallCategoryRenderer extends DefaultListCellRenderer
  {
    private SmallCategoryRenderer()
    {
    }

    public Component getListCellRendererComponent(JList paramJList, Object paramObject, int paramInt, boolean paramBoolean1, boolean paramBoolean2)
    {
      super.getListCellRendererComponent(paramJList, null, paramInt, paramBoolean1, paramBoolean2);
      CategoryInfo localCategoryInfo = (CategoryInfo)paramObject;
      setText(localCategoryInfo.getName());
      setIcon(new ImageIcon(JCatalog.this.tnbcat.getThumbNail(localCategoryInfo.getImage())));
      return this;
    }
  }

  private class CategoriesListModel extends AbstractListModel
  {
    private List m_aCategories;

    public CategoriesListModel(List arg2)
    {
      Object localObject;
   //   localObject=arg2;  //list
      this.m_aCategories = arg2; //ati
    }

    public int getSize()
    {
      return this.m_aCategories.size();
    }

    public Object getElementAt(int paramInt)
    {
      return this.m_aCategories.get(paramInt);
    }
  }

  private class SelectedCategory
    implements ActionListener
  {
    private CategoryInfo category;

    public SelectedCategory(CategoryInfo arg2)
    {
      Object localObject;
      this.category = arg2; //ati
    }

    public void actionPerformed(ActionEvent paramActionEvent)
    {
      JCatalog.this.showSubcategoryPanel(this.category);
    }
  }

  private class SelectedAction
    implements ActionListener
  {
    private ProductInfoExt prod;

    public SelectedAction(ProductInfoExt arg2)
    {
      Object localObject;
      this.prod = arg2; //ati
    }

    public void actionPerformed(ActionEvent paramActionEvent)
    {
      JCatalog.this.fireSelectedProduct(this.prod);
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.catalog.JCatalog
 * JD-Core Version:    0.6.0
 */